package cn.boysoft.module.security.manager.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.boysoft.core.Constants;
import cn.boysoft.core.ServiceCode;
import cn.boysoft.core.annotation.JEEService;
import cn.boysoft.core.manager.impl.ManagerImpl;
import cn.boysoft.core.model.PageParam;
import cn.boysoft.core.model.TreeNode;
import cn.boysoft.core.util.TreeUtil;
import cn.boysoft.module.security.manager.ResourceManager;
import cn.boysoft.module.security.model.entity.ResourceEntity;

/**
 * @author 付厚俊
 * CreateTime 2013-4-5 上午10:29:52
 */

@Service(value="resourceManager")
@Transactional
public class ResourceManagerImpl extends ManagerImpl<ResourceEntity,PageParam> implements ResourceManager<ResourceEntity,PageParam> ,ServiceCode{
	public final static String CODE="04";
	@Override
	public String getServiceClassCode() {
		return Constants.JEE_SYSTEM_CODE+"_"+Constants.JEE_SECURITY_CODE+"_"+CODE+"_";
	}
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	@JEEService(commandId=Constants.JEE_COMMON_CODE_FIRST,name="查询所有资源树",needCheckAuthority=false)
	public Collection<TreeNode> queryAllResourceTree() {
		List<ResourceEntity> allResourceList = queryAllResource();
		Map<String,TreeNode> treeMap = new LinkedHashMap<String, TreeNode>();
		if(allResourceList != null){
			for(ResourceEntity r:allResourceList){
				String id = r.getId();
				TreeNode treeNode = new TreeNode();
				treeMap.put(id, treeNode);
				treeNode.setId(id);
				treeNode.setText(r.getName());
				ResourceEntity parent = r.getParent();
				if(parent != null){
					treeNode.setParentId(parent.getId());
				}
			}
			return (Collection<TreeNode>) TreeUtil.buildTree(treeMap.values());
		}
		return null;
	}
	@Override
	@Transactional(readOnly=true)
	public List<ResourceEntity> queryResourceByUserId(String userId) {
		String hql = " select distinct(res) from  UserEntity u join u.roles r join r.resources res where u.id = ? and res.enabled = ?";
		List<Object> values = new ArrayList<Object>(2);
		values.add(userId);
		values.add(Boolean.TRUE);
		List<ResourceEntity> resources = queryList(hql, values);
		return resources;
	}

	@Override
	public List<ResourceEntity> queryAllResource() {
		String hql = "  from  ResourceEntity res where res.enabled = 'Y' order by res.sortNo asc";
		List<Object> values = new ArrayList<Object>(1);
		values.add(Boolean.TRUE);
		List<ResourceEntity> result = queryList(hql, values);
		return result;
	}

	@Override
	public List<ResourceEntity> queryResourceOfRole(String roleId) {
		String hql = " select distinct(res) from  RoleEntity r  join r.resources res  where r.id = ? and  res.enabled = ?  ";
		List<Object> values = new ArrayList<Object>(2);
		values.add(roleId);
		values.add(Boolean.TRUE);
		List<ResourceEntity> result = queryList(hql, values);
		return result;
	}

}
